package team526.videosave;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import team526.vo.FileTransferVo;

import java.io.*;

@SpringBootApplication
@RestController
public class VideoSaveApplication {
    @Value("${storepath.video}") String path;

    public static void main(String[] args) {
        SpringApplication.run(VideoSaveApplication.class, args);
    }

    @Bean
    public Queue faQueue() {
        return new Queue("file_save_queue");
    }

    @Bean
    public Binding bindingFa() {
        return BindingBuilder
                .bind(faQueue())
                .to(new FanoutExchange("amq.fanout"));
    }

    @RabbitListener(queues = "file_save_queue",containerFactory = "simpleRabbitListenerContainerFactory")
    @RabbitHandler
    public void  fanConsume(@Payload FileTransferVo fileTransferVo, Channel channel) throws IOException {
//        channel.basicQos(10);//不可用
        System.out.println(fileTransferVo.getStorage());
        File dir = new File(path);
            if (!dir.exists()) {
            dir.mkdir();
        }
        File serverFile = new File(dir.getAbsolutePath() + File.separator + fileTransferVo.getStorage());
        BufferedOutputStream bfoStream = new BufferedOutputStream(new FileOutputStream(serverFile));
        bfoStream.write(fileTransferVo.getFile());
        bfoStream.close();
    }

    @Autowired
    CachingConnectionFactory connectionFactory;

    @Bean
    public SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory(){
        SimpleRabbitListenerContainerFactory factory=new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPrefetchCount(10);
        return factory;
    }
}
